﻿#include "tcpthread.h"
#include "macro.h"

/*
Thread::Thread()
{
}

void Thread::run()
{

    exec();

}
TCPWorker::TCPWorker()
{
    cmdSend.resize(8);
    cmdSend[0] = 0x01;
    cmdSend[1] = 0x04;
    cmdSend[2] = 0x00;
    cmdSend[3] = 0x0A;
    cmdSend[4] = 0x00;
    cmdSend[5] = 0x10;
    cmdSend[6] = 0xD1;
    cmdSend[7] = 0xC4;
    //qDebug()<< QThread::currentThreadId() <<endl;
    // tcp
    tcpSocket = new QTcpSocket();
    tcpSocket->setParent(0);
    connect(tcpSocket,&QTcpSocket::connected,[=](){
        qDebug() << "server connected" <<endl;
    });
    tcpSocket->connectToHost(TO_STR(HOST_IP), HOST_PORT);
    if(!tcpSocket->waitForConnected(3000))
    {
        qDebug() << "connection fail"<<endl;
    }
    //connect(tcpSocket, &QTcpSocket::readyRead, this, &TCPWorker::readData, Qt::QueuedConnection);

    m_timer = new QTimer();
    m_timer->setTimerType(Qt::PreciseTimer);
    m_timer->setInterval(DTU_INTERVAL);
    connect(m_timer, &QTimer::timeout, this, &TCPWorker::handleTimeout, Qt::QueuedConnection);
    m_timer->start();
}

void TCPWorker::handleTimeout()
{
    //send request
    qDebug()<< "TCP write"<<QThread::currentThreadId() <<endl;
    tcpSocket->write(cmdSend);
}

void TCPWorker::readData()
{
    static int cnt = 0;
    cnt++;
    if(DEBUG)
    {
        begin = clock();
    }
    qDebug()<< "TCP read"<<QThread::currentThreadId()<<endl;
    QByteArray data = tcpSocket->readAll();


    if(cnt == 10)
    {

        for(int i=0;i<4;i++)
            emit sigDTUUpdate(i, data.constData()[i]);
        cnt = 0;
    }
    if(DEBUG)
    {
        end = clock();
        qDebug() << end - begin<<endl;
    }
}

TCPWorker::~TCPWorker()
{
    m_timer->stop();
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
}

*/



TCPThread::TCPThread()
{
    moveToThread(this);
}

void TCPThread::run()
{
    qDebug() << "tcp "<<QThread::currentThreadId()<<endl;
    cmdSend.resize(8);
    cmdSend[0] = 0x01;
    cmdSend[1] = 0x04;
    cmdSend[2] = 0x00;
    cmdSend[3] = 0x0A;
    cmdSend[4] = 0x00;
    cmdSend[5] = 0x10;
    cmdSend[6] = 0xD1;
    cmdSend[7] = 0xC4;
    //qDebug()<< QThread::currentThreadId() <<endl;
    // tcp
    tcpSocket = new QTcpSocket();
    tcpSocket->setParent(0);
    connect(tcpSocket,&QTcpSocket::connected,[=](){
        qDebug() << "server connected" <<endl;
    });
    tcpSocket->connectToHost(TO_STR(HOST_IP), HOST_PORT);
    if(!tcpSocket->waitForConnected(3000))
    {
        qDebug() << "connection fail"<<endl;
    }
    else
    {
        connect(tcpSocket, &QTcpSocket::readyRead, this, &TCPThread::readData, Qt::QueuedConnection);

        m_timer = new QTimer();
        m_timer->setTimerType(Qt::PreciseTimer);
        m_timer->setInterval(DTU_INTERVAL);
        connect(m_timer, &QTimer::timeout, this, &TCPThread::handleTimeout, Qt::QueuedConnection);
        m_timer->start();
    }
    exec();
}

TCPThread::~TCPThread()
{
    m_timer->stop();
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    delete tcpSocket;
    delete m_timer;
}

void TCPThread::handleTimeout()
{
    //send request
    //qDebug()<< "TCP write"<<QThread::currentThreadId() <<endl;
    tcpSocket->write(cmdSend);
}

void TCPThread::readData()
{
    static int cnt = 0;
    cnt++;
    if(DEBUG)
    {
        begin = clock();
    }
    //qDebug()<< "TCP read"<<QThread::currentThreadId()<<endl;
    const QByteArray data = tcpSocket->readAll();
    if(cnt == DTU_DISPLAY_N / 10)
    {
        for(int i=0;i<4;i++) // pack in one
            emit sigDTUUpload(i, data.constData()[i]);
    }

    if(cnt == DTU_DISPLAY_N)
    {
        for(int i=0;i<4;i++)// pack in one
            emit sigDTUUpdate(i, data.constData()[i]);
        cnt = 0;
    }



    if(DEBUG)
    {
        end = clock();
        qDebug() << end - begin<<endl;
    }
}










